<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <h3>
            <input type="checkbox" :checked=checkAll @click=checkall>全选
        </h3>
        <h3>
            爱好
            <span v-for="item in list" :key=item.id>
                <input type="checkbox" name="hobby" :value="item.value" v-model="item.checked">{{item.msg}}
            </span>
        </h3>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            checkAll: false,
            list: [
                {id: 1, value: 1, msg: "篮球", checked: false},
                {id: 2, value: 2, msg: "单车", checked: false},
                {id: 3, value: 3, msg: "游泳", checked: false},
                {id: 4, value: 4, msg: "足球", checked: false}
            ]
        },
        watch: {
            list: {
                deep: true, //深度监听每条数据
                handler(newList) {
                    this.checkAll = this.list.every(item => item.checked);
                }
            }
        },
        methods: {
            checkall() {
                // console.log(1);
                this.checkAll = window.event.target.checked;
                this.list.forEach(item => {
                    item.checked = this.checkAll;
                });
            }
        }
    })
</script>

</html>